package com.leetcode.p700;

public class Test740 {

    public int deleteAndEarn(int[] nums) {
        int max = 0;
        for (int num : nums) {
            max = Math.max(max, num);
        }

        int[] sum = new int[max + 1];
        for (int val : nums) {
            sum[val] += val;
        }

        return rob(sum);
    }

    public int rob(int[] nums) {
        if (nums.length == 0) {
            return nums[0];
        }

        if (nums.length == 1) {
            return nums[0];
        }

        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int pre = nums[0];
        int next = Math.max(nums[0], nums[1]);
        int result = 0;

        for (int i = 2; i < nums.length; i++) {
            result = Math.max(nums[i] + pre, next);
            pre = next;
            next = result;
        }

        return next;
    }

    public static void main(String[] args) {
        Test740 test = new Test740();
        int result = test.deleteAndEarn(new int[]{2, 2, 3, 3, 3, 4});
        System.out.println(result);
    }

}
